//
//  CreateRoomLayer.m
//  Maze1
//
//  Created by student on 10/10/11.
//  Copyright 2011 tina. All rights reserved.
//

#import "CreateRoomLayer.h"
#import "RoomList.h"
#define CREATROOM 1
#define GETBACK 2

@implementation CreateRoomLayer

@synthesize mapID;
@synthesize nForces;
@synthesize idField;
@synthesize nForcesField;
@synthesize bg;


- (id) init{
    self = [super init];
    if (self != nil) {
        [self setupMenu];
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        
        EAGLView *_view = [[CCDirector sharedDirector]openGLView];
        
		idField = [[UITextField alloc]initWithFrame:CGRectMake(winSize.width*0.3, 300, 200.0, 50.0)];
        idField.transform = CGAffineTransformRotate(idField.transform, (M_PI *2));
        idField.adjustsFontSizeToFitWidth = YES;
        idField.placeholder = @"<Enter Your Name>";
		idField.backgroundColor = [UIColor clearColor];
		idField.textAlignment = UITextAlignmentCenter;
        idField.keyboardType = UIKeyboardTypeDefault;
        idField.returnKeyType = UIReturnKeyDone;
		idField.clearButtonMode = UITextFieldViewModeNever;
        idField.borderStyle = UITextBorderStyleRoundedRect;
        [_view addSubview: idField];
		
		nForcesField = [[UITextField alloc]initWithFrame:CGRectMake(winSize.width*0.3, 400, 200.0, 50.0)];
        nForcesField.transform = CGAffineTransformRotate(nForcesField.transform, (M_PI *2));
        nForcesField.adjustsFontSizeToFitWidth = YES;
        nForcesField.placeholder = @"<Enter Your Name>";
		nForcesField.backgroundColor = [UIColor clearColor];
		nForcesField.textAlignment = UITextAlignmentCenter;
        nForcesField.keyboardType = UIKeyboardTypeDefault;
        nForcesField.returnKeyType = UIReturnKeyDone;
		nForcesField.clearButtonMode = UITextFieldViewModeNever;
        nForcesField.borderStyle = UITextBorderStyleRoundedRect;
        //[nForcesField setSecureTextEntry:YES];
        [_view addSubview: nForcesField];
        
        //create bg;
        bg = [CCSprite spriteWithFile:@"RoomListBack.png"];
        [bg setScale:0.5];
        bg.position = ccp(winSize.width/2, winSize.height/2);
        [self addChild:bg z:-1];

    }
    return self;
}

-(void)setupMenu
{
    CGSize winSize = [[CCDirector sharedDirector] winSize];
	// Create some menu items
	CCMenuItem * menuItem1 = [CCMenuItemImage itemFromNormalImage:@"createButton.png"
														 selectedImage: @"createPressed.png"
																target:self
															  selector:@selector(transit:)];
	menuItem1.tag = CREATROOM;
   	CCMenuItem * menuItem2 = [CCMenuItemImage itemFromNormalImage:@"GobackButton.png"
														 selectedImage: @"GobackPressed.png"
																target:self
															  selector:@selector(goback:)];
	menuItem2.tag = GETBACK;
    menuItem2.position = ccp(0, 0);
    	// Create a menu and add your menu items to it
	CCMenu * myMenu = [CCMenu menuWithItems:menuItem1, menuItem2, nil];
	
	// Arrange the menu items vertically
	[myMenu alignItemsVerticallyWithPadding:2];
    myMenu.position = ccp(winSize.width*0.6, winSize.height/2);
	
	// add the menu to your scene
	[self addChild:myMenu z:1];
}

-(void)transit:(CCMenuItem*)sender
{
    //delete UITextField;
    
	//connect to server
    PackageParser* aParser = [PackageParser sharedPackageParser];
	if (sender.tag == CREATROOM) 
    {
        sender.isEnabled = NO;
        CCMenuItem* item = (CCMenuItem*)[sender.parent getChildByTag:GETBACK];
        item.isEnabled = NO;
        int mid = [idField.text intValue];
        
        int num = [nForcesField.text intValue];
        [idField removeFromSuperview];
        
        [nForcesField removeFromSuperview];
        
                
        if (aParser.player) {
            [aParser.player creatRoomWithMapId:mid numForces:num];
            aParser.state = PlayerStateCreatingRoom;
        }else
        {
            NSLog(@"there is no player existing now.");
        }
        
    }    aParser = nil;
	//if succeed, switch to roomList page
	//[[CCDirector sharedDirector] replaceScene:[RoomInfo scene]];
	
	//else show connection error page
}
-(void)goback:(CCMenuItem*)sender
{
    
    CCMenuItemImage* item = (CCMenuItemImage*)[[self.parent getChildByTag:999] getChildByTag:1];
    item.isEnabled = YES;
    
    item = (CCMenuItemImage*)[[self.parent getChildByTag:999] getChildByTag:2];
    item.isEnabled = YES;
    
    item = (CCMenuItemImage*)[[self.parent getChildByTag:999] getChildByTag:3];
    item.isEnabled = YES;
   
    item = (CCMenuItemImage*)[[self.parent getChildByTag:999] getChildByTag:4];
    item.isEnabled = YES;
    
    [self.parent removeChild:self cleanup:YES];
}

-(void)dealloc
{
    [mapID release];
    mapID = nil;
    [nForces release];
    nForces = nil;
    if(idField != nil){
        [idField removeFromSuperview];
        [idField release];
        idField = nil;

    }
    
    if(nForcesField != nil){
        [nForcesField removeFromSuperview];
        
        [nForcesField release];
        nForcesField = nil;
    }
    [super dealloc];
}

@end
